package models;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import java.util.*;

import play.db.jpa.Model;

/**
 * @author Andriy
 */
@Entity
public class Trips extends Model implements java.io.Serializable {

        public long provId;
        public long userId;
        public Date timeOfRequest;
        @Enumerated(EnumType.STRING)
        public Status status;
        public Date timePDeclared;
        public Date timeURequested;
        public Date timeURequestedCanceled;
        public Date timePAccepted;
        public Date timePAcceptedCanceled;
        public Date timeUConfirmed;
        public Date timePLocked;
        public Date timePReached;
        public Date timePProvided;
        public Date timePEnded;
        public float ULat;
        public float ULong;
        public float PLat;
        public float PLong;

        public Trips() {
        }

        public Trips(Long provId, float PLat, float PLong) {
                this.provId = provId;
                this.PLat = PLat;
                this.PLong = PLong;
                this.status = Status.p_declared;
                this.save();
                Trips.setDate(provId, this.status);
        }

        public Trips(Long provsId, Long usersId, Date timeOfRequest, Date timePDeclared, Date timeURequested, Date timeURequestedCanceled, Date timePAccepted, Date timePAcceptedCanceled, Date timeUConfirmed, Date timePLocked, Date timePReached, Date timePProvided, Date timePEnded, float ULat, float ULong, float PLat, float PLong) {
                this.provId = provsId;
                this.userId = usersId;
                this.timeOfRequest = timeOfRequest;
                this.status = Status.p_declared;
                this.timePDeclared = timePDeclared;
                this.timeURequested = timeURequested;
                this.timeURequestedCanceled = timeURequestedCanceled;
                this.timePAccepted = timePAccepted;
                this.timePAcceptedCanceled = timePAcceptedCanceled;
                this.timeUConfirmed = timeUConfirmed;
                this.timePLocked = timePLocked;
                this.timePReached = timePReached;
                this.timePProvided = timePProvided;
                this.timePEnded = timePEnded;
                this.ULat = ULat;
                this.ULong = ULong;
                this.PLat = PLat;
                this.PLong = PLong;
        }

        public static void setParam(Status status, long provId) {
                Trips trip = Trips.find("select trip from Trips trip where trip.provId=? and trip.status<>?", provId, Status.ended).first();
                Trips.setDate(provId, status);
                trip.status = status;
                trip.save();
        }

        public static Status getStatus(long provId, long userId) {
                Trips trip = Trips.find("select trip from Trips trip where trip.provId=? and trip.status<>?", provId, Status.ended).first();
                return trip.status;
        }

        public static void setParam(Status status, long provId, long userId) {
                Trips trip = Trips.find("select trip from Trips trip where trip.provId=? and trip.status<>?", provId, Status.ended).first();
                Trips.setDate(provId, status);
                trip.status = status;
                trip.userId = userId;
                trip.save();
        }

		public static void setParam(Status status, long provId, long userId, float lat, float lng) {
			Trips trip = Trips.find("select trip from Trips trip where trip.provId=? and trip.status<>?", provId, Status.ended).first();
			Trips.setDate(provId, status);
			trip.status = status;
			trip.userId = userId;
			trip.ULat = lat;
			trip.ULong = lng;
			trip.save();
		}

        public static void setDate(long provId, Status status){
               Date date = new Date();
               date.getTime();
               Trips trip = Trips.find("select trip from Trips trip where trip.provId=? and trip.status<>?", provId, Status.ended).first();
               if (status == Status.p_declared){
                       trip.timePDeclared = date;
               }
               else if(status == Status.u_requested){
                       trip.timeURequested = date;
               }
               else if(status == Status.p_accepted){
                       trip.timePAccepted = date;
               }
               else if (status == Status.p_acceptedCanceled){
                       trip.timePAcceptedCanceled = date;
               }
               else if (status == Status.u_confirmed){
                       trip.timeUConfirmed = date;
               }
               else if (status == Status.u_canceled){
                       trip.timeURequestedCanceled = date;
               }
               else if (status == Status.p_locked){
                       trip.timePLocked = date;
               }
               else if (status == Status.p_reached){
                       trip.timePReached = date;
               }
               else if (status == Status.ended){
                       trip.timePEnded = date;
               }
               else{
                       System.out.println(" Invalid param status in function Trips.setDate()");
               }
               trip.save();
        }

        public void setParam (Status status, float pLat, float pLong){
                this.status = Status.p_declared;
                this.PLat = pLat;
                this.PLong = pLong;
                this.save();
                Trips.setDate(this.provId, this.status);
        }
}
